package com.sunshine.wrongquestionset.room.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.sunshine.wrongquestionset.room.entity.WrongQuestionBean
import com.sunshine.wrongquestionset.room.entity.WrongQuestionEntity

/**
 * @author sunshine
 * @date 2021/3/26
 * @see com.sunshine.wrongquestionset.room.entity.WrongQuestionEntity
 */
@Dao
interface WrongQuestionDao {
    //插入做题情况
    @Query("INSERT INTO wrong_question(questionId, date, score, totalScore, answerPath) VALUES(:questionId, :date, :score, :totalScore, :answerPath)")
    fun insert(questionId: Int, date: Long, score: Float, totalScore: Float, answerPath: String)

    //通过实体类删除一个做题情况
    @Delete(entity = WrongQuestionEntity::class)
    fun deleteByEntity(wrongQuestionEntity: WrongQuestionEntity)

    //通过题目id删除做题情况
    @Query("DELETE FROM wrong_question WHERE questionId=:questionId")
    fun deleteByQuestion(questionId: Int)

    //删除所有做题情况
    @Query("DELETE FROM wrong_question")
    fun deleteAll()

    //查询所有做题情况
    @Query("SELECT * FROM wrong_question")
    fun selectAll(): LiveData<List<WrongQuestionEntity>?>

    //查询指定题目id的做题情况
    @Query("SELECT * FROM wrong_question WHERE questionId=:questionId")
    fun selectByQuestion(questionId: Int): LiveData<List<WrongQuestionEntity>?>

    @Query("SELECT * FROM wrong_question WHERE date>=:startDate and date<=:endDate")
    fun selectInDate(startDate: Long, endDate: Long): LiveData<List<WrongQuestionEntity>?>

    @Query("SELECT * FROM wrong_question WHERE questionId=:questionId and date>=:startDate and date<=:endDate")
    fun selectInDateAndQuestion(questionId: Int, startDate: Long, endDate: Long): LiveData<List<WrongQuestionEntity>?>

    //联合搜索
    @Query("SELECT wrong_question.id, userId, questionId, date, wrong_question.score, totalScore, wrong_question.answerPath, titlePath, question.answerPath as reallyAnswerPath,familiarity, typeId, questionSetId, subjectId FROM wrong_question,question WHERE wrong_question.questionId=question.id and question.userId=:userId")
    fun selectWrongQuestionBeanByUserId(userId: String): LiveData<List<WrongQuestionBean>?>

    @Query("SELECT wrong_question.id, userId, questionId, date, wrong_question.score, totalScore, wrong_question.answerPath, titlePath, question.answerPath as reallyAnswerPath,familiarity, typeId, questionSetId, subjectId FROM wrong_question,question WHERE wrong_question.questionId=question.id and questionSetId=:setId and wrong_question.date>=:startDate and wrong_question.date<=:endDate")
    fun selectWrongQuestionBeanInSetIdAndInDate(setId: Int, startDate: Long, endDate: Long): LiveData<List<WrongQuestionBean>?>

    @Query("SELECT wrong_question.id, userId, questionId, date, wrong_question.score, totalScore, wrong_question.answerPath, titlePath, question.answerPath as reallyAnswerPath,familiarity, typeId, questionSetId, subjectId FROM wrong_question,question WHERE wrong_question.questionId=question.id and subjectId=:subjectId and wrong_question.date>=:startDate and wrong_question.date<=:endDate")
    fun selectWrongQuestionBeanInSubjectIdAndInDate(subjectId: Int, startDate: Long, endDate: Long): LiveData<List<WrongQuestionBean>?>

    @Query("SELECT wrong_question.id, userId, questionId, date, wrong_question.score, totalScore, wrong_question.answerPath, titlePath, question.answerPath as reallyAnswerPath,familiarity, typeId, questionSetId, subjectId FROM wrong_question,question WHERE wrong_question.questionId=question.id and wrong_question.date>=:startDate and wrong_question.date<=:endDate")
    fun selectWrongQuestionBeanInDate(startDate: Long, endDate: Long): LiveData<List<WrongQuestionBean>?>

    @Query("SELECT wrong_question.id, userId, questionId, date, wrong_question.score, totalScore, wrong_question.answerPath, titlePath, question.answerPath as reallyAnswerPath,familiarity, typeId, questionSetId, subjectId FROM wrong_question,question WHERE wrong_question.questionId=question.id and question.id = :questionId")
    fun selectWrongQuestionBeanByQuestionId(questionId: Int): LiveData<List<WrongQuestionBean>?>

//    //查询指定做题对错情况
//    @Query("SELECT * FROM wrong_question WHERE isRight=:isRight")
//    fun selectByRight(isRight: Boolean): LiveData<List<WrongQuestionEntity>?>

    //根据实体类更新一个做题情况
    @Update(entity = WrongQuestionEntity::class, onConflict = OnConflictStrategy.REPLACE)
    fun updateByEntity(wrongQuestionEntity: WrongQuestionEntity)

//    //更新熟悉程度
//    @Query("UPDATE wrong_question SET familiarity=:familiarity WHERE id=:id")
//    fun updateFamiliarity(id: Int, familiarity: Float)
}